home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2935 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  79 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Dynamically allocating memory for a char*
  5. Date: Sat, 20 Jan 1996 19:19:52 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4drf80$ih1@news.halcyon.com>
  8. References: <4dmn1i$10t@walrus2.walrus.com>
  9. NNTP-Posting-Host: blv-pm3-ip2.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. fjordao@walrus.com (Felipe Jordao) wrote:
  13.  
  14. >Hi,
  15.  
  16. >I have a question about allocating memory for a string as it's passed
  17. >in by the user...(I know this is wrong, but the idea of it ...)
  18.  
  19. >cin >> malloc(sizeof(infile));             :0
  20.  
  21. >Here is the actual code
  22. >----------------------------------------------------------------------
  23.  
  24. >main()
  25. >{
  26. > ifstream InputFile;     // Input file stream
  27. > ofstream OutputFile;    // Output file stream
  28. > ...
  29. > OpenFiles(InputFile, OutputFile);
  30. > ...
  31. >}
  32.  
  33.  
  34. >void OpenFiles(ifstream& InputFile, ofstream& OutputFile)
  35. >{
  36. > char infile[30];        // <---- RIGHT HERE.  this works as it is, 
  37. > char outfile[30];       //       but I would like to use
  38. >                         //       char *infile, *outfile.  When
  39. >                         //       I try, I have not found a way to
  40. >                         //       dynamically allocate memory when the
  41. >                         //       user inputs the file.  I know I can
  42. >                         //       use something like infile = new char[X]
  43. >                         //       but that still leave me with the X limit.  
  44.  
  45. > cout << "Enter the path and filename of" << endl   
  46. >      << "the input file (*.csv): ";                
  47. > cin  >> infile;         // I would like to allocate memory right after 
  48. >                         // user inputs the name of the file.  If I use
  49. >                         // *infile and leave it like this it crashes
  50. >                         // because of unallocated space
  51.  
  52. > InputFile.open(infile);       // Test to see if the input file opens
  53. > if (!InputFile) {
  54. >    cerr << "Cannot open input file!" << endl;
  55. >    exit(1);
  56. >    }
  57.  
  58.  
  59. > cout << "Enter the path and filename of" << endl  // get output file
  60. >      << "the output file (*.bcp): ";              // from user
  61. > cin  >> outfile;
  62.  
  63. > OutputFile.open(outfile);    // Test to see if the output file opens
  64. > if (!OutputFile) {
  65. >    cerr << "Cannot open output file!" << endl;
  66. >    exit(1);
  67. >    }
  68. >}
  69.  
  70. If input truly came from a file (not interactive), you'd load the size
  71. of the string first, then new char[ thesize ].
  72.  
  73. For interactive input, either reserve blocks of _MAX_PATH like you've
  74. been doing or perhaps you can do something with cin.get() to retrieve
  75. one character at a time and realloc as you read.
  76.  
  77.                         --Norm
  78.  
  79.